package com.baizhi.controller;

import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.redis.core.StringRedisTemplate;
import com.baizhi.utils.SmsUtils;
import com.baizhi.vo.MsgVO;

import java.util.concurrent.TimeUnit;

/**
 * @author liukaixuan
 */

@RestController
public class SmsController {

    private static final Logger log = LoggerFactory.getLogger(SmsController.class);

    private StringRedisTemplate stringRedisTemplate;

    private SmsUtils smsUtils;

    @Autowired
    public SmsController(StringRedisTemplate stringRedisTemplate, SmsUtils smsUtils) {
        this.stringRedisTemplate = stringRedisTemplate;
        this.smsUtils = smsUtils;
    }

    @PostMapping("captchas")
    public void captchas(@RequestBody MsgVO msgVO){
        //1.获取接收到的手机号
        String phone = msgVO.getPhone();
        log.info("发送短信的手机号为: {}",phone);
        //2.每次发送验证码之前判断,是否存在timeout_132... timeout_176
        String timeOutKey = "timeout_" + phone;
        if(stringRedisTemplate.hasKey(timeOutKey)){
            throw new RuntimeException("提示: 不允许重复发送!");
        }
        try{
            //3.生成4位随机字符
            String code = RandomStringUtils.randomNumeric(4);
            log.info("发送的验证码: {}",code);
            //4.根据接收手机号以及生成随机字符 发送验证码
            smsUtils.sendMsg(phone,code);
            //5.将验证码放入redis key: phone_132... value:code
            //给key 加入一个前缀
            String phoneKey = "phone_" + phone;
            //10分钟验证有效
            stringRedisTemplate.opsForValue().set(phoneKey,code,10, TimeUnit.MINUTES);
            //6.如果验证码在有效期内,不允许重新发送
            stringRedisTemplate.opsForValue().set(timeOutKey,"true",60,TimeUnit.SECONDS);

        } catch (Exception e){
            e.printStackTrace();
            throw  new RuntimeException("提示: 短信发送失败！");
        }


    }






}
